import api from '@/request/api.js';


// uni.login()封装
const wxLogin = function(mobile) {
	return new Promise((resolve, reject) => {
		uni.login({
			success: res => {
				// 获取sessionkey，openid
				api.getSessionKey({
					'js_code': res.code,
					'mobile':mobile
				}).then(res => {
					console.log('登录信息接口返回:', res)
					api.setUserInfo({
						'openid': res.data.openid,
						'mobile':mobile
					}).then(res => {
						console.log(res)
						 let userInfo={
							 'openid': res.data.openid,
							 'nickName': res.data.nickname,
							 'avatarUrl': res.data.avatar,
							 'token': res.data.token,
							 'is_auth': res.data.is_auth,
							 'uid': res.data.uid,
							 'mobile': res.data.mobile,
							 'update_time': res.data.update_time,
							 'nft_address': res.data.nft_address
						 }
						//将用户信息存入本地缓存
						uni.setStorageSync('userInfo',userInfo)
						// uni.setStorage({
						// 			key: 'userInfo',
						// 			data: {
						// 				'openid': res.data.openid,
						// 				'nickName': res.data.nickname,
						// 				'avatarUrl': res.data.avatar,
						// 				'token': res.data.token,
						// 				'is_auth': res.data.is_auth,
						// 				'uid': res.data.uid,
						// 				'mobile': res.data.mobile,
						// 				'update_time': res.data.update_time,
						// 				'nft_address': res.data.nft_address
						// 			},
						// 			complete: (res) => {
						// 			console.log(res)	
						// 			}
						// 		});
						/* 判断用户是否实名认证 */
						/* 更改用户登陆状态 */
						uni.setStorageSync('hasLogin',true)
						/* 设置延迟返回首页 */
						if(res.code==true){
							let userInfo=uni.getStorageInfoSync('userInfo')
							if(userInfo.mobile==''){
								console.log(111)
								this.show=true
							}else{
								console.log(222)
								uni.setStorageSync('hasLogin',true)
								uni.reLaunch({
									url:'../index/index'
								})
							}
						}
					
					}).catch(err => {
						/* console.log(err) */
					
					})
					
					uni.hideLoading();

				}).catch(err => {
					console.log(err)
				})
				console.log(res)
				if (res.code) {
					resolve(res.code)
					// 

				} else {
					reject(res.errMsg);
				}
			}

		})
	})
}
/*微信小程序登录*/
const wechatAppLogin = function() {
	/*登录提示*/

	uni.showLoading("正在授权")
	uni.getUserProfile({
		desc: '获取用户授权',
		success: res => {
			console.log(res.userInfo)
			let userInfo = res.userInfo;
			let openid=uni.getStorageSync('openid')
			wxLogin().then(res => {
				// 引用uni.login()封装
				console.log(res)
				/* 获取成功给后台返回用户信息 */
				

			})
		}
	})
}

// 获取手机号授权
const getPhoneNumber = function(event) {
	console.log(111)
	let that = this;
	let code = event.detail.code; //获取手机code
	console.log(code)
	wxLogin().then(res=>{
		//给后台返回解析手机号的code
		api.getPhone({
			'code':code
		}).then(res=>{
			console.log(res)
			let phone = res.data.phone_info.phoneNumber;
			uni.setStorageSync('phone',phone)
			if(res.code==200){
				uni.switchTab({
					url:'pages/index/index'
				})
			}
		})
	})
	
}
/* 订阅消息 */
const orderMessage=function(e){
	wx.requestSubscribeMessage({
	  tmplIds: ['Gm9WoDJuw67SapN2Kr1T6M7u0VKWEjQdRoQvhOpOnHk'],
	  success: (res) => {
	  	console.log(res)
	  }
	})
}
module.exports = {
	wechatAppLogin,
	getPhoneNumber,
	wxLogin,
	orderMessage
}
